/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.binary.HoughLinesP;
import com.cv4j.core.binary.Threshold;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.Line;

import java.util.ArrayList;
import java.util.List;

/**
 * 直线检测
 *
 * @since 2021-05-17
 */
public class LineDetectionAbilitySlice extends BaseAbilitySlice {
    private Image image0;
    private Image image1;
    private Image image2;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_line_detection;
    }

    @Override
    protected void initComponents(Intent intent) {
        image0 = (Image) findComponentById(ResourceTable.Id_image0);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        PixelMap pixelMap = PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_lines).get();
        image0.setPixelMap(pixelMap);

        CV4JImage cv4jimage = new CV4JImage(pixelMap);
        Threshold threshold = new Threshold();
        final int colorsize = 255;
        threshold.process((ByteProcessor) cv4jimage.convert2Gray().getProcessor(),
            Threshold.THRESH_OTSU, Threshold.METHOD_THRESH_BINARY, colorsize);
        PixelMap newBitmap = cv4jimage.getProcessor().getImage().toBitmap();
        image1.setPixelMap(newBitmap);

        HoughLinesP houghLinesP = new HoughLinesP();
        List<Line> lines = new ArrayList<>();
        final int ten = 10;
        final int num = 12;
        final int fifty = 50;
        houghLinesP.process((ByteProcessor) cv4jimage.getProcessor(), num, ten, fifty, lines);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = newBitmap.getImageInfo().size;
        PixelMap bm2 = PixelMap.create(newBitmap, options);
        Canvas canvas = new Canvas(new Texture(bm2));
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        final float width = 4f;
        paint.setStrokeWidth(width);
        paint.setColor(Color.RED);
        for (Line line : lines) {
            canvas.drawLine(line.x1, line.y1, line.x2, line.y2, paint);
        }
        image2.setPixelMap(bm2);
    }
}
